var json = [ {
	id : "1",
	name : "总店",
	list : [ {
		id : "11",
		name : "总店仓库总店仓库总店仓库1",
		isdefault : "true"
	}, {
		id : "12",
		name : "总店仓库2",
		isdefault : "false"
	}, {
		id : "13",
		name : "总店仓库3",
		isdefault : "false"
	}, {
		id : "11",
		name : "总店仓库4",
		isdefault : "false"
	} ]
}, {
	id : "2",
	name : "门店一",
	list : [ {
		id : "11",
		name : "门店一仓库1",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店一仓库2",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店一仓库3",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店一仓库4",
		isdefault : "false"
	} ]
}, {
	id : "3",
	name : "门店二",
	list : [ {
		id : "11",
		name : "门店二仓库1",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店二仓库2",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店二仓库3",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店二仓库4",
		isdefault : "false"
	} ]
}, {
	id : "4",
	name : "门店三",
	list : [ {
		id : "11",
		name : "门店三仓库1",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店三仓库2",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店三仓库3",
		isdefault : "false"
	}, {
		id : "11",
		name : "门店三仓库4",
		isdefault : "false"
	} ]
}, {
	id : "5",
	name : "仓库四"
} ];
var str = "";
var getTreeList = function(r, d, p, g, m) {
	var k = $("#salesman").val();
	var s = $("#productversion", parent.document).val();
	if ("3" == s) {
		k = "0";
	}
	if ("1" == m) {
		k = "";
	}
	var b = null;
	var f = new Array();
	var t = null;
	if (r != null) {
		for (var l = 0; l < r.length; l++) { // r:json数据
			if (typeof r[l]["childList"] != "undefined"
					&& r[l]["childList"] != null
					&& r[l]["childList"].length >= 1) {
				t = new Array();
				var a = r[l]["childList"];
				for (var h = 0; h < a.length; h++) {
					b = a[h].usertype;
					if ("1" == k) {
						if ("0" == b) {
							a[h]["name"] = "员工：" + a[h]["name"];
							t.push(a[h]);
						} else {
							if ("1" == b) {
								a[h]["name"] = "导购员：" + a[h]["name"];
								t.push(a[h]);
							} else {
								t.push(a[h]);
							}
						}
					} else {
						if ("1" == b) {
						} else {
							t.push(a[h]);
						}
					}
				}
				r[l]["childList"] = t;
			}
			b = r[l].usertype;
			if ("1" == k) { // k:1 user;
				if ("0" == b) { // b:0 员工； b:1导购员; 其他：...
					r[l]["name"] = "员工：" + r[l]["name"];
					f.push(r[l]);
				} else {
					if ("1" == b) {
						r[l]["name"] = "导购员：" + r[l]["name"];
						f.push(r[l]);
					} else {
						f.push(r[l]);
					}
				}
			} else {
				if ("1" == b) {
				} else {
					f.push(r[l]);
				}
			}
		}
	}
	var c = "";
	str = "";
	str += '<div class="firstList"><ul id="ls-ul1-' + d + '">';
	if (g == 1) {
		if (typeof f != "undefined" && f != null && f[0]["remark1"] != null) {
			c = f[0]["remark1"];
		}
		str += '<input type="hidden" class="ls-input-init-text" value="全部" />';
		str += '<input type="hidden" class="ls-input-init-val" value="" />';
		str += '<input type="hidden" class="ls-input-init-parentval" value="'
				+ c + '" />';
		str += '<input type="hidden" class="ls-input-init-parenttext" value="" />';
		str += '<li class="normal" id="ls-li-' + d
				+ '" onclick="levelSelect.choose(\'' + d + "', '全部','', '" + c
				+ '\');"><span title="全部">全部</span></li>';
	}
	if (f != null && f != undefined) {
		for (var l = 0; l < f.length; l++) {
			try {
				if (typeof f[l]["childList"] == "undefined"
						|| f[l]["childList"] == null
						|| f[l]["childList"].length < 1) {
					if (p == 0 && f[l]["selected"] == "1") {
						str += '<input type="hidden" class="ls-input-init-text" value="'
								+ f[l]["name"] + '" />';
						str += '<input type="hidden" class="ls-input-init-val" value="'
								+ f[l]["id"] + '" />';
						str += '<input type="hidden" class="ls-input-init-parentval" value="'
								+ f[l]["remark1"] + '" />';
						str += '<input type="hidden" class="ls-input-init-parenttext" value="" />';
					}
					str += '<li class="normal" id="ls-li-' + d + "-"
							+ f[l]["id"]
							+ '" onclick="levelSelect.choose(\'' + d + "', '"
							+ f[l]["name"] + "', '" + f[l]["id"] + "','"
							+ f[l]["remark1"] + "',null," + f[l].userType
							+ ');"><span title="' + f[l]["name"] + '">'
							+ f[l]["name"] + "</span></li>";
				} else {
					str += '<li class="normal haveChild" id="ls-li-' + d + "-"
							+ f[l]["id"] + '" t="' + f[l]["id"]
							+ '"><span title="' + f[l]["name"] + '">'
							+ f[l]["name"] + "</span></li>";
				}
			} catch (n) {
			}
		}
		str += "</ul></div>";
		var q = 0;
		for (var l = 0; l < f.length; l++) {
			try {
				if (typeof f[l]["childList"] != "undefined"
						&& f[l]["childList"] != null
						&& f[l]["childList"].length > 0) {
					var a = f[l]["childList"];
					if (q == 0) {
						str += '<div class="secList" id="ls-li-childList-' + d
								+ "-" + f[l]["id"] + '"><ul>';
						q = 1;
					} else {
						str += '<div class="secList" id="ls-li-childList-' + d
								+ "-" + f[l]["id"]
								+ '" style="display:none"><ul>';
					}
					if (g == 1) {
						str += '<li class="normal" id="ls-li-payout" onclick="levelSelect.choose(\''
								+ d
								+ "', '全部', '','"
								+ f[l]["id"]
								+ "','"
								+ f[l]["name"] + "');\">";
						str += '<span title="全部">全部</span>';
						str += "</li>";
					}
					for (var h = 0; h < a.length; h++) {
						if (p == 0 && a[h]["selected"] == "1") {
							str += '<input type="hidden" class="ls-input-init-text" value="'
									+ a[h]["name"] + '" />';
							str += '<input type="hidden" class="ls-input-init-val" value="'
									+ a[h]["id"] + '" />';
							str += '<input type="hidden" class="ls-input-init-parentval" value="'
									+ f[l]["id"] + '" />';
							str += '<input type="hidden" class="ls-input-init-parenttext" value="'
									+ f[l]["name"] + '" />';
						}
						str += '<li class="normal" id="ls-li-payout-'
								+ a[h]["id"]
								+ '" onclick="levelSelect.choose(\'' + d
								+ "', '" + a[h]["name"] + "', '"
								+ a[h]["id"] + "','" + f[l]["id"] + "','"
								+ f[l]["name"] + "');\">";
						str += '<span title="' + a[h]["name"] + '">'
								+ a[h]["name"] + "</span>";
						str += "</li>";
					}
					str += "</ul></div>";
				}
			} catch (n) {
			}
		}
	}
	return str;
};
var levelSelect = {
	init : function(a, e, d, c, b) {
		$.ajax({
			dataType : "json",
			data : {},
			cache : false,
			async : false,
			url : a,
			type : "post",
			success : function(f) {
				if (d == null || d == undefined) {
					d = 0;
				}
				if (c == null || c == undefined) {
					c = 0;
				}
				$("#levelSelect-" + e + "-list").html(
						getTreeList(f, e, d, c, b));
			},
			error : function() {
				alert("失败");
			}
		});
		$("#levelSelect-" + e).each(function() {
			var i = $(this).find(".ls-input-init-text").eq(0).val();
			var h = $(this).find(".ls-input-init-val").eq(0).val();
			var f = $(this).find(".ls-input-init-parentval").eq(0).val();
			var g = $(this).find(".ls-input-init-parenttext").eq(0).val();
			if (e.indexOf("warehouse") >= 0 && g != "" && g != undefined) {
				$(this).find(".ls-input").eq(0).val(g + ">" + i);
			} else {
				$(this).find(".ls-input").eq(0).val(i);
			}
			$(this).find(".ls-input-val").eq(0).val(h);
			$(this).find(".ls-input-parentval").eq(0).val(f);
		});
		$("#ls-ul1-" + e + " li").bind(
				"mouseenter",
				function() {
					$("#ls-ul1-" + e + " .select").removeClass("select")
							.addClass("normal");
					$(this).removeClass("normal").addClass("select");
					$(".secList").hide();
					$("#ls-li-childList-" + e + "-" + $(this).attr("t")).show();
				});
	},
	show : function(a) {
		levelSelect.hide();
		$("#levelSelect-" + a + " .ls2").hide();
		$("#levelSelect-" + a + " .ls1").show();
	},
	hide : function() {
		$(".levelSelect .ls2").hide();
		$(".levelSelect .ls1").hide();
	},
	select : function(b, a) {
		$("#ls-li-" + b + "-" + a).click();
	},
	choose : function(g, f, e, d, c, a) {
		var b = $("#isnotvalidate").val();
		if (parent.$("#isAdminRole").val() == "false" && "1" != b) {
			if (g.indexOf("user") > -1 & parent.$("#allowview").val() == "0"
					&& $("#levelSelect-" + g + " .ls-input-val").val() != e
					&& a != 1) {
				$(".levelSelect .ls2").hide();
				$(".levelSelect .ls1").hide();
				top.showErrorMsg("您没有查看他人单据权限，所以也不能替他人代开单");
				return;
			}
		}
		if (g.indexOf("warehouse") >= 0 && c != "" && c != null) {
			$("#levelSelect-" + g + " .ls-input").val(c + ">" + f);
		} else {
			$("#levelSelect-" + g + " .ls-input").val(f);
		}
		$("#levelSelect-" + g + " .ls-input-parentval").val(d);
		$("#levelSelect-" + g + " .ls-input-val").val(e).change();
		$("#levelSelect-" + g + " .ls2").hide();
		$("#levelSelect-" + g + " .ls1").hide();
	},
	initshow : function(e, d, c, b, a) {
		if (e.indexOf("warehouse") >= 0 && a != "" && a != null) {
			$("#levelSelect-" + e + " .ls-input").val(a + ">" + d);
		} else {
			$("#levelSelect-" + e + " .ls-input").val(d);
		}
		$("#levelSelect-" + e + " .ls-input-parentval").val(b);
		$("#levelSelect-" + e + " .ls-input-val").val(c);
		$("#levelSelect-" + e + " .ls2").hide();
		$("#levelSelect-" + e + " .ls1").hide();
	}
};